<!--
 * @Author: dumin
 * @Date: 2025-06-27 16:53:17
 * @Description: 上传文件按钮(单个文件上传)
 * @LastEditors: dumin
 * @LastEditTime: 2025-06-29 20:23:41
-->
<template>
  <el-upload :action="action + url" :headers="headers" :show-file-list="false" :limit="1" :file-list="fileList"
    :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" :disabled="uploadLoading"
    accept=".xls,.xlsx">
    <slot :loading="uploadLoading">
    </slot>
  </el-upload>
</template>

<script>
export default {
  props: {
    url: { type: String, default: '' },
  },
  data() {
    return {
      uploadLoading: false,
      fileList: [],
      headers: {
        token: localStorage.getItem('token')
      },
      action: process.env.VUE_APP_BASE_API
    }
  },
  methods: {
    // 导入相关
    beforeUpload() {
      this.uploadLoading = true;
    },
    handleError() {
      this.uploadLoading = false;
    },
    handleSuccess(res, file) {
      this.uploadLoading = false;
      this.fileList = [];
      if(res.code == 1) {
        this.$message.success('导入成功');
        this.$emit('change', res);
      } else {
        this.$message.error(res.message);
      }
    },
  }
}
</script>